﻿using Xejen.Communication.Socket.Abstract;
using Xejen.Communication.Socket.Communicator;
using Xejen.Communication.Socket.Configuration;
using Xejen.Hosting;
using Xejen.Hosting.Extensions;
using Xejen.Logger;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xejen.Communication.Socket.Extensions
{
    /// <inheritdoc cref="ISocketCommunicatorBuilder"/>的实现
    internal sealed class SocketCommunicatorBuilder : CommunicatorBuilder<ISocketCommunicatorBuilder>, ISocketCommunicatorBuilder
    {
        private readonly ILogger _logger;
        private readonly SocketConfig _socketConfig = new SocketConfig();

        /// <inheritdoc/>
        public SocketConfig Config => _socketConfig;

        /// <inheritdoc cref="SocketCommunicatorBuilder"/>
        /// <param name="host"><inheritdoc cref="IAppHost" path="/summary"/></param>
        public SocketCommunicatorBuilder(IAppHost host) : base(host)
        {
            _logger = LoggerManager.CreateLogger<SocketCommunicatorBuilder>();
        }

        /// <inheritdoc/>
        public ISocketCommunicatorBuilder ConfigureSocketConfiguration(Action<SocketConfig> configure)
        {
            Configuration.Bind(nameof(SocketConfig), _socketConfig);

            configure?.Invoke(_socketConfig);

            ConfigureServices((services) =>
            {
                services.AddSingleton(_socketConfig);
            });

            _logger.LogInformation($"完成{nameof(SocketConfig)}配置：{{@value}}", _socketConfig);

            return this;
        }
    }
}
